package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜品分类管理相关接口")
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    @ApiOperation("分类管理分页查询")
    @GetMapping("/page")
    public Result<PageDTO> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类管理分页查询:{}", categoryPageQueryDTO);
        PageDTO<Category> pageDTO = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageDTO);
    }

    @ApiOperation("根据id修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("根据id修改分类:{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @ApiOperation("根据分类id查询")
    @GetMapping("/{id}")
    public Result<Category> editCategory(@PathVariable("id") long id){
        log.info("根据分类id查询:{}", id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result<String> deleteById(Long id){
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("修改分类状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status, @RequestParam("id") Long id){
        log.info("修改分类状态id:{}, status:{}", id, status == StatusConstant.ENABLE ? "启用" : "禁用");
        categoryService.startOrStop(status, id);
        return Result.success();
    }


    // 根据类型查询分类
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}